import { createRouter, createWebHistory } from 'vue-router'
import LoginPage from '@/views/LoginPage.vue'
import { accountStore } from '@/stores/account'

// router是路由管理器 ，控制页面跳转等操作
// route对象是页面元信息
// routeConfig 是主要的route信息，供程序员配置
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'welcome',
      redirect: '/login'
    },
    {
      path: '/login',
      name: 'login',
      component: LoginPage
    },
    {
      path: '/app',
      name: 'app',
      component: () => import('@/views/ApplicationPage.vue'),
      redirect: '/material',
      children: [
        {
          path: '/material',
          name: 'material',
          component: () => import('@/views/MaterialView.vue')
        },
        {
          path: '/transport',
          name: 'transport',
          component: () => import('@/views/TransportView.vue')
        },
        {
          path: '/leaveapply',
          name: 'leaveapply',
          component: () => import('@/views/LeaveApply.vue')
        },
        {
          path: '/leaveaudit',
          name: 'leaveaudit',
          component: () => import('@/views/LeaveAudit.vue')
        },
        {
          path: '/purchaseapplier',
          name: 'purchaseapplier',
          component: () => import('@/views/PurchaseApplier.vue')
        },
        {
          path: '/purchaseauditor',
          name: 'purchaseauditor',
          component: () => import('@/views/PurchaseAuditor.vue')
        },
        {
          path: '/subexponse',
          name: 'subexponse',
          component: () => import('@/views/SubExponse.vue')
        },
        {
          path: '/subexponseaudit',
          name: 'subexponseaudit',
          component: () => import('@/views/SubExponseAudit.vue')
        },
        {
          path: '/practiseone',
          name: 'practiseone',
          component: () => import('@/viewprac/PractiseOne.vue')
        },
        {
          path: '/practisetwo',
          name: 'practisetwo',
          component: () => import('@/viewprac/PractiseTwo.vue')
        },
        {
          path: '/practisethree',
          name: 'practisethree',
          component: () => import('@/viewprac/PractiseThree.vue')
        }
      ]
    }
  ]
})
const whiteList = ['/', '/login']
router.beforeEach((to, from, next) => {
  // console.log(to, from)
  // next()
  const store = accountStore()
  if (whiteList.includes(to.path)) {
    next()
  } else {
    // 访问普通应用页面
    if (store.token) {
      next()
    } else {
      next('/login')
      // router.push("/login")
    }
  }
})
export default router
